package com.aletto.general_store.ui.activities.orders;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.OrderCompany;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.ui.popup.AddChainNodePopup;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.QuickList;

import java.util.List;
import java.util.function.Consumer;

public class OrderCompanyPopup extends BottomPopupView {


    private Consumer<OrderCompany> mConsumer;
    private final OrderCompanyAdapter mAdapter;


    public OrderCompanyPopup(@NonNull Context context, Consumer<OrderCompany> consumer) {
        this(context);
        mConsumer = consumer;
    }

    public OrderCompanyPopup(@NonNull Context context) {
        super(context);

        mAdapter = new OrderCompanyAdapter();
        task(mAdapter).execute();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_order_company;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        findViewById(R.id.iv_close).setOnClickListener(v -> dismiss());

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener((adapter, view, position) -> click(mAdapter.getItem(position)));
    }

    private void click(OrderCompany orderCompany){
        dismiss();
        mConsumer.accept(orderCompany);
    }


    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .5f);
    }

    public SimpleTemplate<List<OrderCompany>> task(OrderCompanyAdapter adapter) {
        return new SimpleTemplate<>() {
            @Override
            public INetTask.View<List<OrderCompany>> view() {
                return new QuickList<>(adapter);
            }

            @Override
            public INetTask.Presenter<List<OrderCompany>, INetTask.View<List<OrderCompany>>> presenter() {
                return new NetSingleTask<>(DataRepository.getInstance().logisticsList());
            }
        };
    }

    public static class OrderCompanyAdapter extends BaseQuickAdapter<OrderCompany, BaseViewHolder> {

        public OrderCompanyAdapter() {
            super(R.layout.item_order_company);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, OrderCompany item) {
            holder.setText(R.id.text_name, item.getName());
        }
    }

}
